package giormi;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.JTable;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Tim
 */
public class viewCustomers {
    private String SQLStatement;
    private Connection conn;
    private String URL;
    private Statement stmt;
    public ResultSet rset;
    protected JTable main;
        
    public viewCustomers(JTable table){
        this.main = table;
    }
    
    public void connect(){
        try{
            DBConnection db = new DBConnection();
            conn = DBConnection.get();
            stmt = conn.createStatement();
        }
        catch(Exception e){
            System.out.println(e.getMessage());
            JOptionPane.showMessageDialog(null,e.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }

    public void disconnect(){
        try{
            stmt.close();
            conn.close();
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }
    
    public void viewCustomers()
    {

        
        int i=0,nCtr=0;
            connect();
            //3
            SQLStatement = "SELECT * FROM customer ORDER BY idcustomer"; 
            try {
            rset = stmt.executeQuery(SQLStatement);
            while(rset.next())
                nCtr++;
            rset.close();
            rset = stmt.executeQuery(SQLStatement);
            
            while(rset.next()){
                main.setValueAt(rset.getInt("idcustomer"),i,0);
                main.setValueAt(rset.getString("c_name"),i,1);

                i++;
            }
            rset.close();
            //5
          disconnect();
            }
             catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }
    
}
